package fr.umlv.ig.adapter;

import java.util.EventObject;

import javax.swing.table.AbstractTableModel;

import fr.umlv.ig.listener.SearchListener;
import fr.umlv.ig.model.Line;
import fr.umlv.ig.model.MetroModelImpl;
import fr.umlv.ig.model.Station;

/**
 * Adapter of a {@link MetroModelImpl} for represents a TableModel.
 * @author jenseric
 *
 */
@SuppressWarnings("serial")
public class StationTableModel extends AbstractTableModel {
	/**
	 * MetroModelImpl.
	 */
	private final FilterModel model;
	
	/**
	 * Titles of the TableModel.
	 */
	private final String[] title= {"Line","Station","Open"};

	/**
	 * index of column represented the lines.
	 */
	public static final int colLine=0;

	/**
	 * index of column represented the stations.
	 */
	public static final int colStation =1;

	/**
	 * index of column represented the statut of the station
	 */
	public static final int colState = 2;

	public StationTableModel(final FilterModel model) {
		this.model = model;
		model.addListener(new SearchListener() {
			@Override
			public void changed(EventObject event, int index1, int index2) {
				changedRow(index1, index2);
			}
		});
	}

	public void changedRow(int index1, int index2) {
		fireTableRowsDeleted(index1, index2);
	
	}

	@Override
	public int getRowCount() {
		return model.size();
	}

	@Override
	public int getColumnCount() {
		return 3;
	}


	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		switch (columnIndex) {
		case colLine:
			return model.getLine(rowIndex);
		case colStation:
			return model.getStation(rowIndex);
		case colState :
			return model.getBoolean(rowIndex);
		default:
			break;
		}
		return null;
	}



	@Override
	public String getColumnName(int col) {
		switch (col) {
		case colLine:
			return title[col];
		case colStation:
			return title[col];
		case colState :
			return title[col];
			
		default:
			break;
		}
		return null;
	}


	@Override
	public Class<?>  getColumnClass(int col){
		switch (col) {
		case colLine:
			return Line.class;
		case colStation:
			return Station.class;
		case colState :
			return Boolean.class;
		default:
			break;
		}
		return null;
	}

	public boolean isCellEditable(int row, int col){
		if(col ==colState) {
			return true;
		}
		return false;

	}

	@Override
	public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
		if(columnIndex == colState) {
			Station station = (Station) getValueAt(rowIndex, 1);
			if((Boolean)aValue==true) {
				model.openStation(station);
			}
			else {
				model.closeStation(station);
			}
		}
	}

	public void selected(Station station) {
		model.selected(station);
		
	}

	public void filtre(String prefix) {
		model.filtre(prefix);
		
	}
	
	
//	public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
//
//		final JFrame frame = new JFrame("Test clic");
//		frame.setSize(400, 300);
//		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//
//		MetroModelFactory metroFactory = MetroModelFactory.newInstance("conf.xml");
//		final MetroModelImpl model = metroFactory.getMetroModelImpl();
//
//
//		final FilterModel filterModel = new FilterModel(model);
//		final JTable table = new JTable(new StationTableModel2(filterModel));
//		final TableRowSorter<TableModel> sorter=new TableRowSorter<TableModel>(table.getModel());
//
//		table.setDefaultRenderer(Object.class, StationTableRenderer.METROTABLECELLRENDERER);
//
//		table.setDefaultEditor(Boolean.class, new DefaultCellEditor(new JComboBox(new Boolean[]{true,false})));
//		
//		
//
//		JTextField textField = new JTextField("");
//
//		textField.addCaretListener(new CaretListener() {
//
//			@Override
//			public void caretUpdate(CaretEvent e) {
//				String s = ((JTextField)e.getSource()).getText();
//				filterModel.filtre(s);
//
//			}
//		});
//		
//		filterModel.addListener(new SearchListener() {
//			
//			@Override
//			public void changed(EventObject event, int index1, int index2) {
//				((StationTableModel2)table.getModel()).changed(index1, index2);
//			}
//		});
//		
//		
//		TableColumnModel modelColumn = table.getColumnModel();
//		modelColumn.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
//			
//			@Override
//			public void valueChanged(ListSelectionEvent e) {
//				// TODO Auto-generated method stub
//				
//			}
//		});
//
//		textField.setBorder(BorderFactory.createLineBorder(Color.BLUE, 5));
//		frame.add(textField,BorderLayout.NORTH);
//		
//		table.addMouseListener(new MouseAdapter() {
//			@Override
//			public void mouseClicked(MouseEvent e) {
//				System.out.println(table.getSelectedRow());
//			}
//		});
//
//
//
//		frame.getContentPane().add(new JScrollPane(table));
//		frame.setVisible(true);
//	}
}
